use "actions T1 - T6.dta", clear

keep if treatment>4

expand 90
sort session Subject Period
by session Subject Period: gen TimeStamp=_n

sort session Subject Period TimeStamp

save pippo, replace

use "chat rich communication.dta", clear

drop if Period==.

gen KAK=1 if KAKat<6
gen CHK=1 if CHKat<6


gen type=0
replace type=KAK if KAK==CHK

gen message1=type==1

drop if message1==0


***we drop observations were more message C were sent in the same value of TimaStamp

sort session Period SubjectID TimeStamp
by session Period SubjectID TimeStamp: keep if _n==1




keep session Period SubjectID message1 TimeStamp	seq

rename SubjectID Subject

sort session Period Subject TimeStamp

joinby session Period Subject TimeStamp using pippo, unmatched(both)


**************************
**************************
**************************
  
 replace message1=0 if message1==. 
  
 local i=1
 while `i'<33 {
 gen mess`i'=message1 if Subject==`i'
 local i=`i'+1
 }
 

 local i=1
 while `i'<33 {

 sort session Period TimeStamp
 by session Period TimeStamp: egen mm`i'=total(mess`i')
 
 local i=`i'+1
 }
 
 local i=1
 while `i'<33 {

 replace mess`i'= mm`i'
 
 local i=`i'+1
 }
 
 
 drop mm1-mm32
 
 
sort session MatchingGroup NetworkGroup
egen NetGroup=group(session MatchingGroup NetworkGroup)



local i=1
while `i'<33 {
sort session Subject Period TimeStamp

by session Subject Period: replace mess`i'=mess`i'+mess`i'[_n-1] if _n>1

local i=`i'+1
}




 
local i=1
while `i'<33 {
sort session Subject Period TimeStamp

by session Subject Period: gen F_mess`i'=mess`i' if _n==1
by session Subject Period: replace F_mess`i'=mess`i'[_n]+mess`i'[_n-1] if _n>1



local i=`i'+1
}

 
 local i=1
while `i'<33 {

replace message1=2 if Subject==`i'&F_mess`i'>1


local i=`i'+1
}

 
 local j=1
while `j'<5 {


local i=1
while `i'<33 {

sort session Subject Period TimeStamp 
replace MessagesReceived`j'=mess`i'[_n-1] if   LinkedSubjects`j'==`i'
replace MessagesReceived`j'=0 if TimeStamp==1

local i=`i'+1
 }
 
local j=`j'+1
 }
 
gen MessRec= MessagesReceived1+MessagesReceived2+MessagesReceived3+MessagesReceived4
 
 replace MessRec=1 if MessRec>1
 

sort NetGroup Period TimeStamp Subject

by NetGroup Period TimeStamp: egen pippo2=total(MessRec)


*** To obtain the numbers and test for period=1, activate command "drop if Period>1" below  
*** To obtain the numbers and test for period<6, activate command "drop if Period>5" below  
*** To obtain the numbers and test for the reduced sample, activate command "drop if pippo2==8|pippo2==0" below  


preserve

drop if message1>1

*drop if Period>1
*drop if Period>5
*drop if pippo2==8|pippo2==0

gen message_r_0=message1==1&MessRec==0
gen message_r_1=message1==1&MessRec==1

sort session MatchingGroup
by session MatchingGroup: gen num_obs=_N
by session MatchingGroup: egen num_obs_r_1=total(MessRec)
by session MatchingGroup: gen index2=_n==1
gen num_obs_r_0=num_obs-num_obs_r_1

sort session MatchingGroup
by session MatchingGroup:egen num_mess_r_0=total(message_r_0)
by session MatchingGroup:egen num_mess_r_1=total(message_r_1)

gen p_0=num_mess_r_0/num_obs_r_0
gen p_1=num_mess_r_1/num_obs_r_1

signrank p_0= p_1 if index2==1
signtest p_0= p_1 if index2==1

replace p_0=. if MessRec==1
replace p_1=. if MessRec==0

sum p_0 p_1

restore
